#include "ive.h"

#ifdef MOCK
#else
    #ifdef __cplusplus //而这一部分就是告诉编译器，如果定义了__cplusplus(即如果是cpp文件，
    extern "C" { //因为cpp文件默认定义了该宏),则采用C语言方式进行编译
    #include "sample_ive_canny.h"
    #include "hi_type.h"
    }
    #endif

#endif

//int IVE::SaveImage(const char *path, ive_AlgoParam *algoParam)
int IVE::SaveImage(const char *path, AlgoParam *algoParam)
{
    HI_S32 s32Ret = 0;
    FILE* pFp;
    /*open*/
    pFp = fopen(path, "wb");
    if(pFp == HI_NULL) {
        printf("Error,Open file %s failed!\n", path);
        return s32Ret;
    }

    printf("before %s \n", __FUNCTION__);

    s32Ret = SAMPLE_COMM_IVE_CreateImage((IVE_IMAGE_S*)(algoParam->get_img_ptr()), IVE_IMAGE_TYPE_U8C1,
                                         (HI_U32)algoParam->get_width(),(HI_U32)algoParam->get_height());
    if(s32Ret<0) {
        printf("Error(%#x),Create Src Image failed!\n", s32Ret);
        return s32Ret;
    }

    /*write*/
    s32Ret = SAMPLE_COMM_IVE_WriteFile((IVE_IMAGE_S*)(algoParam->get_img_ptr()), pFp);
    if(s32Ret<0) {
        printf("Error(%#x),Write src file failed!\n",s32Ret);
        return s32Ret;
    }
}

#if 1
int IVE::LoadImage(const char *path, int height, int width, AlgoParam *algoParam) {
    HI_S32 s32Ret = 0;
    FILE* pFp;
    algoParam->set_width(width) ;
    algoParam->set_height(height);

    /*create Image*/
    SAMPLE_COMM_IVE_CheckIveMpiInit();
#if 1
    s32Ret = SAMPLE_COMM_IVE_CreateImage((IVE_IMAGE_S*)(algoParam->get_img_ptr()), IVE_IMAGE_TYPE_U8C1,
            (HI_U32)width,(HI_U32)height);
    if(s32Ret<0) {
        printf("Error(%#x),Create Src Image failed!\n", s32Ret);
        return s32Ret;
    }
#endif

    /*open*/
    pFp = fopen(path, "rb");
    if(pFp == HI_NULL) {
        printf("Error,Open file %s failed!\n", path);
        return s32Ret;
    }

    /*read*/
    s32Ret = SAMPLE_COMM_IVE_ReadFile((IVE_IMAGE_S*)(algoParam->get_img_ptr()), pFp);
    if(s32Ret<0) {
        printf("Error(%#x),Read src file failed!\n",s32Ret);
        return s32Ret;
    }
#endif
    printf("load image end\n");
    return 0;
}


void IVE::canny(AlgoParam *src, AlgoParam *dst, double low_threshold, double high_threshold,
        int apertureSize, bool L2gradient)

{
#ifdef MOCK
#else
    SAMPLE_IVE_CANNY_INFO_S s_stCannyInfo;



    HI_U16 u32Width = src->get_width();
    HI_U16 u32Height = src->get_height();
    //HI_CHAR* pchSrcFileName = "./data/input/canny/canny.yuv";
    HI_S32 s32Ret=0;
    memset(&s_stCannyInfo,0,sizeof(s_stCannyInfo));

    //拷贝？
    memcpy(&(s_stCannyInfo.stSrc) , (IVE_IMAGE_S*)(src->get_img_ptr()), sizeof(IVE_IMAGE_S));

    //low , high ,apertureSize . put them into the following
    //Need to modify
    printf("canny init start\n");
    s32Ret = SAMPLE_IVE_Canny_Init_HAWK(&s_stCannyInfo,
                                   u32Width, u32Height, low_threshold, high_threshold,
                                   apertureSize);
    printf("canny init end\n");
    SAMPLE_CHECK_EXPR_GOTO(HI_SUCCESS != s32Ret, CANNY_FAIL,
                           "Error(%#x),SAMPLE_IVE_Canny_Init failed!\n", s32Ret);

    if (!L2gradient)
    {
        s32Ret =  SAMPLE_IVE_Part_Canny(&s_stCannyInfo);
    }
    else
    {
        s32Ret =  SAMPLE_IVE_Complate_Canny(&s_stCannyInfo);
    }
    printf("canny end\n");
    if (HI_SUCCESS == s32Ret)
    {
        SAMPLE_PRT("Process success!\n");
    }
    /*Add ezio*/
    dst->set_height( s_stCannyInfo.stEdge.u32Height);
    dst->set_width( s_stCannyInfo.stEdge.u32Width ) ;
    memcpy((IVE_IMAGE_S*)(dst->get_img_ptr()) , &(s_stCannyInfo.stEdge), sizeof(IVE_IMAGE_S));

    printf("canny uninit start\n");
    SAMPLE_IVE_Canny_Uninit(&s_stCannyInfo);
    memset(&s_stCannyInfo,0,sizeof(s_stCannyInfo));

    CANNY_FAIL:
    SAMPLE_COMM_IVE_IveMpiExit();
#endif
}
#if 0
void IVE::sobel(ive_AlgoParam *src, ive_AlgoParam *dst, int ddepth, int dx, int dy, int ksize, double scale, double delta,
                int borderType) {

}
#endif